package com.breaktian.assemble.customviews.dialog;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;


import com.breaktian.assemble.R;

import java.io.Serializable;

/**
 * 收银台Dialog
 * <p>
 * Created by An on 17/3/3.
 */
public class FragmentDialog
        extends DialogFragment {

    public static final String TAG = FragmentDialog.class.getName();

    /**
     * Positive按钮
     */
    public static final int BUTTON_POSITIVE = 1;

    /**
     * Negative按钮
     */
    public static final int BUTTON_NEGATIVE = -1;

    /**
     * Dialog按钮点击监听器
     */
    public interface OnClickListener {

        /**
         * 按钮点击回调
         *
         * @param dialog {@link FragmentDialog}
         * @param which  哪个按钮被点击，如：{@link FragmentDialog#BUTTON_POSITIVE)
         */
        void onClick(FragmentDialog dialog, int which);
    }

    /**
     * 构建器
     */
    public static class Builder implements Serializable {

        /* 标题 */
        String title;
        /* 消息 */
        String message;
        /* Positive按钮 */
        String positiveText;
        OnClickListener positiveListener;
        /* Negative按钮 */
        String negativeText;
        OnClickListener negativeListener;

        public Builder() {
        }

        /**
         * 设置标题
         *
         * @param title 标题
         * @return 构建器 {@link FragmentDialog.Builder}
         */
        public FragmentDialog.Builder setTitle(@NonNull String title) {
            this.title = title;
            return this;
        }

        /**
         * 设置消息
         *
         * @param message 消息
         * @return 构建器 {@link FragmentDialog.Builder}
         */
        public FragmentDialog.Builder setMessage(@NonNull String message) {
            this.message = message;
            return this;
        }

        /**
         * 设置Positive按钮监听器
         *
         * @param text     Positive按钮文案
         * @param listener 监听器
         * @return 构建器 {@link FragmentDialog.Builder}
         */
        public FragmentDialog.Builder setPositiveButton(@NonNull String text, @NonNull FragmentDialog.OnClickListener listener) {
            this.positiveText = text;
            this.positiveListener = listener;
            return this;
        }

        /**
         * 设置Negative按钮监听器
         *
         * @param text     Negative按钮文案
         * @param listener 监听器
         * @return 构建器 {@link FragmentDialog.Builder}
         */
        public FragmentDialog.Builder setNegativeButton(@NonNull String text, @NonNull FragmentDialog.OnClickListener listener) {
            this.negativeText = text;
            this.negativeListener = listener;
            return this;
        }

        /**
         * 创建Dialog
         *
         * @return Dialog
         */
        public FragmentDialog create() {
            Bundle bundle = new Bundle();
            bundle.putSerializable("builder", this);

            FragmentDialog cashierDialog = new FragmentDialog();
            cashierDialog.setArguments(bundle);

            return cashierDialog;
        }

    }

    /* 标题 */
    private String mTitle;
    /* 消息 */
    private String mMessage;
    /* Positive按钮 */
    private String mPositiveText;
    private OnClickListener mPositiveListener;
    /* Negative按钮 */
    private String mNegativeText;
    private OnClickListener mNegativeListener;

    //============= 视图 =============

    private View mContentView;
    private ViewStub mContentViewStub;
    private ViewStub mButtonsViewStub;

    @Nullable
    private TextView mTitleTextView;
    @Nullable
    private TextView mMessageTextView;
    @Nullable
    private Button mPositiveButton;
    @Nullable
    private Button mNegativeButton;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FragmentDialog.Builder builder = (FragmentDialog.Builder) getArguments().getSerializable("builder");
        if (builder != null) {
            mTitle = builder.title;
            mMessage = builder.message;
            mPositiveText = builder.positiveText;
            mPositiveListener = builder.positiveListener;
            mNegativeText = builder.negativeText;
            mNegativeListener = builder.negativeListener;
        }
        getArguments().clear();//解决页面切换到后台，反序列化的异常
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        mContentView = inflater.inflate(R.layout.bm_fragment_dialog_layout, null);
        mContentViewStub = (ViewStub) mContentView.findViewById(R.id.content_view_stub);
        mButtonsViewStub = (ViewStub) mContentView.findViewById(R.id.buttons_view_stub);


        //内容区
        int contentRes = R.layout.bm_fragment_dialog_content_layout;
        if (TextUtils.isEmpty(mTitle)) {
            contentRes = R.layout.bm_fragment_dialog_content_no_title_layout;
        }
        mContentViewStub.setLayoutResource(contentRes);

        //按钮区
        int buttonsRes;
        if (mPositiveListener != null && mNegativeListener != null) {
            buttonsRes = R.layout.bm_fragment_dialog_pair_buttons_layout;
        } else if (mPositiveListener == null && mNegativeListener == null) {
            buttonsRes = 0;
        } else {
            buttonsRes = R.layout.bm_fragment_dialog_single_button_layout;
        }
        if (buttonsRes != 0) {
            mButtonsViewStub.setLayoutResource(buttonsRes);
        }

        //Inflate
        View contentView = mContentViewStub.inflate();
        mTitleTextView = (TextView) contentView.findViewById(R.id.title_text_view);
        mMessageTextView = (TextView) contentView.findViewById(R.id.message_text_view);

        View buttonsView = mButtonsViewStub.inflate();
        mPositiveButton = (Button) buttonsView.findViewById(R.id.positive_button);
        mNegativeButton = (Button) buttonsView.findViewById(R.id.negative_button);

        return mContentView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        if (!TextUtils.isEmpty(mTitle)) {
            mTitleTextView.setText(mTitle);
        }
        if (!TextUtils.isEmpty(mMessage)) {
            mMessageTextView.setText(mMessage);
        }

        //设置按钮监听器

        if (mPositiveListener != null && mPositiveButton != null) {
            mPositiveButton.setText(mPositiveText);
            mPositiveButton.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mPositiveListener.onClick(FragmentDialog.this, BUTTON_POSITIVE);
                        }
                    });
        }
        if (mNegativeListener != null && mNegativeButton != null) {
            mNegativeButton.setText(mNegativeText);
            mNegativeButton.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mNegativeListener.onClick(FragmentDialog.this, BUTTON_NEGATIVE);
                        }
                    });
        }
    }

}
